library(fixest);library(AER);library(stringr);library(knitr)
rm(list=setdiff(ls(), "twd")) 
options(warn=-1)

data<-readRDS(paste(twd,"data/made_data/vreg_wgeos_race.rds",sep=""))


## In person 20
data$in_person_primary_20<-ifelse(data$voting_method_200303 == "P",1,0)
## Abs early 20
data$abs_early_primary_20<-ifelse(data$voting_method_200303 %in% c("A","E"),1,0)
## In person 18
data$in_person_general_18<-ifelse(data$voting_method_181106 == "P",1,0)
## Abs early 18
data$abs_early_general_18<-ifelse(data$voting_method_181106 %in% c("A","E"),1,0)
## Take First Difference
data$diff_in_person<-data$in_person_primary_20-data$in_person_general_18
data$diff_abs_early<-data$abs_early_primary_20-data$abs_early_general_18
### get the minimum of the distance to a consolidated (including super sites)
data$distance_realized_min<-apply(cbind(data$distance_realized,data$distancegov1,data$distancegov2),1,min,na.rm=T)
## Difference between assigned and realized distances (in logs and levels)
data$ln_dist_change<-ifelse(data$moved==1,log(data$distance_realized_min)-log(data$distance_assigned),0)
data$lev_dist_change<-ifelse(data$moved==1,data$distance_realized_min-data$distance_assigned,0)
### Difference between assigned and realized size # of assigned voters (in logs and levels)
data$ln_diff_N<-log(data$realized_N)-log(data$assigned_N)
data$lev_diff_N<-data$realized_N-data$assigned_N

#### Create differences in turnout for all previous elections and run regression ######

## 18-18b

data$in_person_primary_18<-ifelse(data$voting_method_180802== "P",1,0)
data$abs_early_primary_18<-ifelse(data$voting_method_180802 %in% c("A","E"),1,0)

data$diff_in_person_18_18<-data$in_person_general_18-data$in_person_primary_18
data$diff_abs_early_18_18<-data$abs_early_general_18-data$abs_early_primary_18

## 18-16

data$in_person_general_16<-ifelse(data$voting_method_161108== "P",1,0)
data$abs_early_general_16<-ifelse(data$voting_method_161108 %in% c("A","E"),1,0)

data$diff_in_person_18_16<-data$in_person_primary_18-data$in_person_general_16
data$diff_abs_early_18_16<-data$abs_early_primary_18-data$abs_early_general_16

## 16-16a

data$in_person_primary_16a<-ifelse(data$voting_method_160804== "P",1,0)
data$abs_early_primary_16a<-ifelse(data$voting_method_160804 %in% c("A","E"),1,0)

data$diff_in_person_16_16a<-data$in_person_general_16-data$in_person_primary_16a
data$diff_abs_early_16_16a<-data$abs_early_general_16-data$abs_early_primary_16a

## 16a-16b

data$in_person_primary_16b<-ifelse(data$voting_method_160301== "P",1,0)
data$abs_early_primary_16b<-ifelse(data$voting_method_160301 %in% c("A","E"),1,0)


data$diff_in_person_16_16b<-data$in_person_primary_16a-data$in_person_primary_16b
data$diff_abs_early_16_16b<-data$in_person_primary_16a-data$abs_early_primary_16b

## 16b-14

data$in_person_general_14<-ifelse(data$voting_method_141104== "P",1,0)
data$abs_early_general_14<-ifelse(data$voting_method_141104 %in% c("A","E"),1,0)

data$diff_in_person_16_14<-data$in_person_primary_16b-data$in_person_general_14
data$diff_abs_early_16_14<-data$in_person_primary_16b-data$abs_early_general_14

## 14-14a

data$in_person_primary_14<-ifelse(data$voting_method_140807== "P",1,0)
data$abs_early_primary_14<-ifelse(data$voting_method_140807 %in% c("A","E"),1,0)

data$diff_in_person_14_14<-data$in_person_general_14 - data$in_person_primary_14
data$diff_abs_early_14_14<-data$abs_early_general_14 - data$abs_early_primary_14

## 14a-12

data$in_person_general_12<-ifelse(data$voting_method_121106== "P",1,0)
data$abs_early_general_12<-ifelse(data$voting_method_121106 %in% c("A","E"),1,0)

data$diff_in_person_14_12<-data$in_person_primary_14 - data$in_person_general_12
data$diff_abs_early_14_12<-data$abs_early_primary_14 - data$abs_early_general_12

### create realized turnout measures

turnout_realized_20<-tapply(data$in_person_primary_20,data$realized_poll_name,sum)
turnout_realized_20<-data.frame(turnout_realized_20,names(turnout_realized_20))
names(turnout_realized_20)<-c("actual_turnout_realized_20","realized_poll_name")

data<-merge(data,turnout_realized_20,by="realized_poll_name")

turnout_realized_18<-tapply(data$in_person_general_18,data$polling_place_text_name,sum)
turnout_realized_18<-data.frame(turnout_realized_18,names(turnout_realized_18))
names(turnout_realized_18)<-c("actual_turnout_realized_18","polling_place_text_name")

data<-merge(data,turnout_realized_18,by="polling_place_text_name")


data$diff_ln_actual_turnout<-log(data$actual_turnout_realized_20)-log(data$actual_turnout_realized_18)
data$ln_actual_turnout<-log(data$actual_turnout_realized_20)


summary(ft5a<-feols(diff_in_person~moved+consolidated+ln_dist_change+diff_ln_actual_turnout+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early,data=data,cluster~polling_place_text_name))
summary(ft5b<-feols(diff_in_person~moved+consolidated+ln_dist_change+ln_diff_N+diff_ln_actual_turnout+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early,data=data,cluster~polling_place_text_name))
summary(ft5e<-feols(diff_in_person~moved+consolidated+lev_dist_change+ln_diff_N+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early,data=data,cluster~polling_place_text_name))
summary(ft5f<-feols(diff_in_person~moved+consolidated+ln_dist_change+I(lev_diff_N/1000)+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early,data=data,cluster~polling_place_text_name))
summary(ft5g<-feols(diff_in_person~moved+consolidated+lev_dist_change+I(lev_diff_N/1000)+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early,data=data,cluster~polling_place_text_name))

log_print(
  esttex(list(ft5e,ft5f,ft5g,ft5a,ft5b), ci=.95,
       coefstat = "confint",signif.code = NA, 
       keep = c("moved","consolidated","ln_dist_change","lev_dist_change",
                "ln_diff_N","lev_diff_N","diff_ln_actual_turnout"),
       fitstat = c("n")
       )
)
